package org.dromara.rank.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.rank.domain.AddScoreRequest;
import org.dromara.rank.domain.vo.UserRankVO;
import org.dromara.rank.service.IUserRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("")
public class UserRankController {
    @Autowired
    private IUserRankService userRankService;

    /**
     * 获取用户排名
     * @param pageNum
     * @param pageSize
     * @return
     */

    @RequestMapping("/getRankPage")
    public R<IPage<UserRankVO>> list(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize){
        IPage<UserRankVO> page = userRankService.getRankPage(pageNum, pageSize);
        return R.ok(page);
    }

    /**
     * 获取用户排名详情
     * @param userId
     * @return
     */
    @GetMapping("/query/{userId}")
    public R<UserRankVO> getUserRankDetail(@PathVariable Long userId){
        UserRankVO userRankVO = userRankService.getUserRankDetail(userId);
        return R.ok(userRankVO);
    }

    @PostMapping("/addScore")
    public R<String> addScore(@RequestBody AddScoreRequest request){
        try{

            userRankService.addScoreToUser(request.getUserId(), request.getItemName(), request.getProfile());
            return R.ok("添加成功");
        }catch (Exception e){
            return R.fail("添加失败", e.getMessage());
        }
    }


}
